//
//  HotTopicViewController.m
//  FlowerRainGround
//
//  Created by zfc on 16/4/25.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HotTopicViewController.h"
#import "RecommendModel.h"
#import "MyPostCollectionView.h"
#import "HotTopicTableViewCell.h"
#import "CustomerModel.h"
@interface HotTopicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *hotTopicArray;
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, assign) float h;
@end

@implementation HotTopicViewController


- (NSMutableArray *)hotTopicArray {
    if (!_hotTopicArray) {
        self.hotTopicArray = [NSMutableArray array];
    }
    return _hotTopicArray;
}

- (NSMutableArray *)picArray {
    if (!_picArray) {
        self.picArray = [NSMutableArray array];
    }
    return _picArray;
}

- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:@"http://app.htxq.net/servlet/UserBbsServlet?currentPageIndex=0&action=findBbsByTags&pageSize=20" parameters:@{@"tag":self.str} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"^^^%@",dataDic);
        
        NSArray *array = dataDic[@"result"];
        for (NSDictionary *dic in array) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            CustomerModel *customerModel = [[CustomerModel alloc] init];
            [customerModel setValuesForKeysWithDictionary:dic[@"customer"]];
            model.customerModel = customerModel;
            [self.hotTopicArray addObject:model];

        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.hotTopicTableView reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = [NSString stringWithFormat:@"#%@#",self.str];
    
    [self handleData];
    
    [self.hotTopicTableView registerClass:[HotTopicTableViewCell class] forCellReuseIdentifier:@"PPP"];
    // Do any additional setup after loading the view from its nib.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.hotTopicArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    RecommendModel *model = self.hotTopicArray[indexPath.row];
    self.picArray = [[model.attachment componentsSeparatedByString:@","] mutableCopy];
    CGFloat imagewidth = (kWidth-24)/3;
    _h = 0;
    if (self.picArray.count == 1) {
        _h = imagewidth*3 + 164;
    } else if(self.picArray.count == 2) {
        _h = (kWidth-22)/2 + 160;
    }else {
        NSInteger number = [self getRowNumber];
        _h = number * imagewidth + (number-1)*2 + 160;
    }
    
    return _h;
}

//获取列数
- (NSInteger)getRowNumber {
    
    if (self.picArray.count % 3 == 0) {
        return self.picArray.count / 3;
    } else {
        return self.picArray.count / 3 + 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    RecommendModel *model = self.hotTopicArray[indexPath.row];
    HotTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPP" forIndexPath:indexPath];
    NSArray *subViews = cell.subviews;
    for (UIView *view in subViews) {
        [view removeFromSuperview];
    }
    [cell createCellWithModel:model];

    return cell;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
